#include<vector>
#include<algorithm>
using namespace std;

class Solution {
private:
    struct Section {
        int index, left, right;
        Section(int index, int left, int right) :index(index), left(left),right(right) {

        }
    };


public:
    vector<int> findRightInterval(vector<vector<int>>& intervals) {
        vector<Section>sects;
        for (int i = 0; i < intervals.size(); ++i) {
            sects.emplace_back(i, intervals[i][0], intervals[i][1]);
        }

        sort(sects.begin(), sects.end(), [](const Section& left, const Section& right) {
            return left.left < right.left;
            });

        vector<int>res(intervals.size(), -1);
        for (int i = 0; i < sects.size(); ++i) {
            int left = i + 1, right = sects.size() - 1;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                if (sects[mid].left == sects[i].right) {
                    res[sects[i].index] = sects[mid].index;
                    break;
                }
                if (sects[mid].left > sects[i].right)
                    right = mid - 1;
                else
                    left = mid + 1;
            }
            if (res[sects[i].index] == -1&&left!= sects.size()) 
                res[sects[i].index] = sects[left].index;
        }
        return res;
    }
};